package Services;

import Entity.Appointment;
import Entity.Person;
import Entity.Veterinarian;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class VeterinarianService implements Serializable {

    public VeterinarianService(EntityManagerFactory emf) {
        this.emf = emf;  
        personService = new PersonService (emf);
    }
    private EntityManagerFactory emf = null;
    PersonService personService = null;
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Veterinarian veterinarian) {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(veterinarian);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Veterinarian veterinarian) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            veterinarian = em.merge(veterinarian);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Long id = veterinarian.getId();
                if (findVeterinarian(id) == null) {
                    throw new NonexistentEntityException("The veterinarian whit id" + id + "no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Long id) throws NonexistentEntityException {

        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Veterinarian veterinarian;
            try {
                veterinarian = em.getReference(Veterinarian.class, id);
                veterinarian.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The veterinarian whit id" + id + "no longer exists.", enfe);
            }
            em.remove(veterinarian);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Veterinarian> findVeterinarianEntities() {
        return findVeterinarianEntities(null,true, -1, -1);
    }
    
    public List<Veterinarian> findVeterinarianEntities(String name) {
        return findVeterinarianEntities(name,true, -1, -1);
    }

    public List<Veterinarian> findVeterinarianEntities(int maxResults, int firstResult) {
        return findVeterinarianEntities(null,false, maxResults, firstResult);
    }

    private List<Veterinarian> findVeterinarianEntities(String name, boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Veterinarian.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);

                if (name != null) {
                    for (int i = 0; i < q.getResultList().size(); i++) {
                        Veterinarian vet = (Veterinarian) q.getResultList().get(i);
                        Query query = em.createNativeQuery("Select id from person where name=" + name);
                        vet.setVeterinarianList((Veterinarian) findVeterinarian(query));
                    }
                }
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
     
    private Veterinarian findVeterinarian(Long id) {
        EntityManager em = getEntityManager();
        try {
            Veterinarian veterinarian = em.find(Veterinarian.class, id);
            return veterinarian;
        } finally {
            em.close();
        }
    }
    private List<Person> findVeterinarian(Query query) {
        List<Person> per = new ArrayList<>();
        for (int j = 0; j < query.getResultList().size(); j++)  {
            Long perId = (Long) query.getResultList().get(j);
            per.add(findVeterinarian(perId));
        }
        return per;
    }   
}
